package application;

import java.io.Serializable;

/**
 * This class implements basic functionality: automatic updates
 * based on time. If this class is inherited and method update()
 * implemented, it's possible to do various graphic or functional
 * effects very easy.
 * 
 * @author Tomas Zima
 */
public abstract class AutomaticallyUpdatable implements Serializable
{
	/**
	 * Constructor.
	 * Set update time.
	 * 
	 * @param updateTime
	 * 	Update time in milliseconds.
	 */
	public AutomaticallyUpdatable(int updateTime)
	{
		this();
		this.setUpdateTime(updateTime);
	}
	
	/**
	 * Constructor.
	 * Do not set update time.
	 */
	public AutomaticallyUpdatable()
	{
		lastUpdate = System.currentTimeMillis();
	}
	
	/**
	 * Set update time.
	 * 
	 * @param time
	 * 	Update time in milliseconds.
	 */
	public void setUpdateTime(int time)
	{
		this.updateTime = time;
	}
	
	/**
	 * This method should be called as fast as possible to
	 * achieve desired functionality.
	 */
	public void refresh()
	{
		if (System.currentTimeMillis() > lastUpdate + updateTime)
		{
			this.lastUpdate = System.currentTimeMillis();
			this.update();
		}
	}
	
	/**
	 * This method should be implemented by the client. 
	 */
	protected abstract void update();
	
	private int		updateTime;
	private long	lastUpdate;
}
